package mercadologico.entities.tables;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

import mercadologico.dao.PersistenceMarca;

import exceptions.PersistenceExceptionMessage;
import generic.interfaces.GenericPersistence;
import generic.interfaces.IDEntity;
import generic.interfaces.PersistInFile;

@Entity
public class Marca implements IDEntity<Integer>, GenericPersistence<PersistenceMarca, Integer, Marca>, PersistInFile<Marca>, Comparable<Marca> {

	public static PersistenceMarca persist = new PersistenceMarca();
	
	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	private Integer id;
	private String descricao;
	
	
	public Integer getId() {
		return this.id;
	}
	
	public void setId(Integer id) {
		this.id = id;
	}
	public String getDescricao() {
		return descricao;
	}
	public void setDescricao(String descricao) {
		this.descricao = descricao;
	}
	
	public int compareTo(Marca marca) {
		return getDescricao().compareTo(marca.getDescricao());
	}
	
	public Marca getInstanceFromArray(String[] str) {
		for (String string : str) {
			if (string == null)
				return null;
			if (string.trim().length() == 0)
				return null;
		}

		if (str != null && str.length == 2) {
			Marca marca = new Marca();
			marca.setId(Integer.parseInt(str[0]));
			marca.setDescricao(str[1]);
			return marca;
		}
		return null;
	}
	
	public Collection<Marca>  order(ArrayList<Marca> entities) {
		Collections.sort(entities);
		return entities;
	}
	
	public int removeAll() throws PersistenceExceptionMessage {
		return DAOInstance().removeAll();
	}
	
	public PersistenceMarca DAOInstance() throws PersistenceExceptionMessage {
		if (persist == null) {
			persist = new PersistenceMarca();
		}
		return persist;
	}
	
	public Marca find(Integer id) throws PersistenceExceptionMessage {
		return DAOInstance().find(id);
	}
	
	public void persist() throws PersistenceExceptionMessage {
		DAOInstance().persist(this);
	}
	
	public void remove() throws PersistenceExceptionMessage {
		DAOInstance().remove(this);
	}
	
	public Marca merge() throws PersistenceExceptionMessage {
		return DAOInstance().merge(this);
	}
	
	public ArrayList<Marca> findAll() throws PersistenceExceptionMessage {
		return (ArrayList<Marca>) DAOInstance().findAll();
	}
	
	public String toString() {
		return getDescricao();
	}
	
	public boolean equals(Object obj) {
		if (obj instanceof Marca)
			return getId().equals(((Marca) obj).getId());
		return false;
	}
	
	public ArrayList<String> getArrayAsString() throws PersistenceExceptionMessage {
		ArrayList<String> strs = new ArrayList<String>();
		for (Marca ma : order((ArrayList<Marca>) DAOInstance().findAll())) {
			strs.add(ma.getId() +"|" +ma.getDescricao());
		}
		return strs;
	}
}
